package org.swingDemo;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FollowerRangeModel extends ConverterRangeModel implements ChangeListener {

    ConverterRangeModel sourceModel;

    public FollowerRangeModel(ConverterRangeModel sourceModel) {
        this.sourceModel = sourceModel;
        this.sourceModel.addChangeListener(this);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        fireStateChanged();
    }

    @Override
    public int getMaximum() {
        int modelMax = sourceModel.getMaximum();
        double mulitiplyBy = sourceModel.getMultiplier() / this.getMultiplier();
        return (int)(modelMax * mulitiplyBy);
    }

    @Override
    public void setMaximum(int newMaximum) {
        sourceModel.setMaximum((int)(newMaximum * (this.getMultiplier() / sourceModel.getMultiplier())));
    }

    @Override
    public int getValue() {
        return (int)getDoubleValue();
    }

    @Override
    public void setValue(int value) {
        setDoubleValue((double)value);
    }

    @Override
    public double getDoubleValue() {
        return sourceModel.getDoubleValue() * sourceModel.getMultiplier() / this.getMultiplier();
    }

    @Override
    public void setDoubleValue(double newValue) {
        sourceModel.setDoubleValue(newValue * this.getMultiplier() / sourceModel.getMultiplier());
    }

    @Override
    public int getExtent() {
        return super.getExtent();
    }

    @Override
    public void setExtent(int extent) {
        super.setExtent(extent);
    }

    @Override
    public void setRangeProperties(int value, int extent, int min, int max, boolean adjusting) {
        double multiplyBy = this.getMultiplier() / sourceModel.getMultiplier();
        sourceModel.setRangeProperties(value * multiplyBy, extent, min,
                (int)(max * multiplyBy), adjusting);
    }
}
